#include "ImageFormats.h"
#include <iostream>
using namespace std;
//image+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//placement----------------------------
image::image(){
    window = NULL;
}
void image::removeFrom(SDL_Surface* surf){
}
void image::move(int xPos, int yPos){
    destPos.x = xPos;
    destPos.y = yPos;
    if(window != NULL){
        draw();
    }
}
void image::slide(int xDist, int yDist){
    destPos.x += xDist;
    destPos.x += yDist;
    if(window != NULL){
        draw();
    }
}
//manipulators-------------------------
void image::setDrawSurface(SDL_Surface* surf){
    if(!good()) return;
    if(surf == NULL) return;
    window = surf;
}
void image::setDrawSurface(cairo_surface_t* cs){
    if(!good()) return;
    if(cs == NULL) return;
    cairoSurf = cs;
}
void image::setVisible(bool vis){
    visible = vis;
}
//handlers-----------------------------
bool image::onEvent(SDL_Event* event){
    list<eventHandler*>::iterator itr;
    if(event->type == SDL_MOUSEBUTTONDOWN || event->type == SDL_MOUSEBUTTONUP){
        int cx = event->button.x;
        int cy = event->button.y;
        if((cx <= (xPosition() + width()) && cx >= xPosition()) &&
                (cy <= (yPosition() + height()) && cy >= yPosition())){
            for(itr = eHandlerStack.begin(); itr != eHandlerStack.end(); itr++){
                if((*itr)->onEvent(event)){
                    return true;
                }
            }
        }
        return false;
    }
    return false;
}
void image::onUpdate(float time){
}
void image::addEventHandler(eventHandler* e){
    eHandlerStack.push_back(e);
}
void image::addUpdateHandler(updateHandler* u){
    uHandlerStack.push_back(u);
}
void image::removeEventHandler(eventHandler* e){
    eHandlerStack.remove(e);
}
void image::removeUpdateHandler(updateHandler* u){
    uHandlerStack.remove(u);
}
//access-------------------------------
bool image::good(){
    return (window != NULL);
}
bool image::isVisible(){
    return visible;
}
int image::xPosition(){
    return destPos.x;
}
int image::yPosition(){
    return destPos.y;
}
//image=========================================================
//PNGimage------------------------------------------------------
//constructors-------------------------
PNGimage::PNGimage(string img){
    //create image------
    imageName = img;
    SDL_Surface* temp = NULL;
    temp = IMG_Load(imageName.c_str());
    imageSurface = SDL_DisplayFormatAlpha(temp);
    SDL_FreeSurface(temp);
    //set up the rects--
    destPos.x = 0;
    destPos.y = 0;
    dims.x = 0;
    dims.y = 0;
    dims.w = imageSurface->w;
    dims.h = imageSurface->h;
    //set visible-------
    visible = true;
}
PNGimage::~PNGimage(){
    SDL_FreeSurface(imageSurface);
}
//placement----------------------------
void PNGimage::draw(){
    if(!good()) return;
    if(!visible) return;
    cout.flush();
    SDL_BlitSurface(imageSurface, &dims, window, &destPos);
}
//access-------------------------------
bool PNGimage::good(){
    return (imageSurface != NULL);
}
int PNGimage::width(){
    return imageSurface->w;
}
int PNGimage::height(){
    return imageSurface->h;
}
//PNGimage======================================================
//VECTimage-----------------------------------------------------
//constructors-------------------------
VECTimage::VECTimage() : VectorBody(){

}
VECTimage::VECTimage(vector<VectorPoint*> points) : VectorBody(points){

}
VECTimage::VECTimage(list<VectorPoint*> points) : VectorBody(points){

}
VECTimage::VECTimage(VectorBody* points) : VectorBody(points){

}
//placemanet---------------------------
void VECTimage::draw(){
    cout << getCenter()->getX() << " " << getCenter()->getY() << endl;

    //var block----
    vector<VectorPoint*>::iterator itr = getBegin();
    cairo_t* ct = cairo_create(cairoSurf);
    //var block====
    
    //first point
    cairo_move_to(ct, (*itr)->getX(), (*itr)->getY());
    itr++;

    //point loop
    for(/*empty*/; itr != getEnd(); itr++){
        cairo_line_to(ct, (*itr)->getX(), (*itr)->getY());
    }

    //close the form
    cairo_set_source_rgb(ct, r, g, b);
    cairo_close_path(ct);
    if(fill){
        cairo_fill(ct);
    } else {
        cairo_stroke(ct);
    }
    cairo_destroy(ct);
}
void VECTimage::move(int x, int y){
    cout << "vect - draw -----\n";
    cout << x << " " << y << endl;
    cout << "=================\n";
   moveCenter(x, y);
}
void VECTimage::slide(int x, int y){
    slideCenter(x, y);
}
//access-------------------------------
bool VECTimage::good(){
    return true;
}
int VECTimage::width(){
    return getMaxRadius();
}
int VECTimage::height(){
    return getMaxRadius();
}
//manipulators-------------------------
void VECTimage::setColor(float r, float g, float b){
    this->r = r;
    this->g = g;
    this->b = b;
}
void VECTimage::setFill(bool f){
    fill = f;
}
//events-------------------------------
bool VECTimage::onEvent(SDL_Event* event){
    list<eventHandler*>::iterator itr;

    for(itr = eHandlerStack.begin(); itr != eHandlerStack.end(); itr++){
        if((*itr)->onEvent(event)) return true;
    }
    return false;
}
//VECTimage=====================================================